﻿using System;
using System.Collections.Generic;

namespace UniRx.Operators
{
	// Token: 0x02000336 RID: 822
	internal class WhenAllObservable<T> : OperatorObservableBase<T[]>
	{
		// Token: 0x06001106 RID: 4358 RVA: 0x0004E710 File Offset: 0x0004CB10
		public WhenAllObservable(IObservable<T>[] sources) : base(false)
		{
			this.sources = sources;
		}

		// Token: 0x06001107 RID: 4359 RVA: 0x0004E720 File Offset: 0x0004CB20
		public WhenAllObservable(IEnumerable<IObservable<T>> sources) : base(false)
		{
			this.sourcesEnumerable = sources;
		}

		// Token: 0x06001108 RID: 4360 RVA: 0x0004E730 File Offset: 0x0004CB30
		protected override IDisposable SubscribeCore(IObserver<T[]> observer, IDisposable cancel)
		{
			if (this.sources != null)
			{
				return new WhenAllObservable<T>.WhenAll(this.sources, observer, cancel).Run();
			}
			IList<IObservable<T>> list = this.sourcesEnumerable as IList<IObservable<T>>;
			if (list == null)
			{
				list = new List<IObservable<T>>(this.sourcesEnumerable);
			}
			return new WhenAllObservable<T>.WhenAll_(list, observer, cancel).Run();
		}

		// Token: 0x04000A08 RID: 2568
		private readonly IObservable<T>[] sources;

		// Token: 0x04000A09 RID: 2569
		private readonly IEnumerable<IObservable<T>> sourcesEnumerable;

		// Token: 0x02000337 RID: 823
		private class WhenAll : OperatorObserverBase<T[], T[]>
		{
			// Token: 0x06001109 RID: 4361 RVA: 0x0004E786 File Offset: 0x0004CB86
			public WhenAll(IObservable<T>[] sources, IObserver<T[]> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.sources = sources;
			}

			// Token: 0x0600110A RID: 4362 RVA: 0x0004E7A4 File Offset: 0x0004CBA4
			public IDisposable Run()
			{
				this.length = this.sources.Length;
				if (this.length == 0)
				{
					this.OnNext(new T[0]);
					try
					{
						this.observer.OnCompleted();
					}
					finally
					{
						base.Dispose();
					}
					return Disposable.Empty;
				}
				this.completedCount = 0;
				this.values = new T[this.length];
				IDisposable[] array = new IDisposable[this.length];
				for (int i = 0; i < this.length; i++)
				{
					IObservable<T> observable = this.sources[i];
					WhenAllObservable<T>.WhenAll.WhenAllCollectionObserver observer = new WhenAllObservable<T>.WhenAll.WhenAllCollectionObserver(this, i);
					array[i] = observable.Subscribe(observer);
				}
				return StableCompositeDisposable.CreateUnsafe(array);
			}

			// Token: 0x0600110B RID: 4363 RVA: 0x0004E864 File Offset: 0x0004CC64
			public override void OnNext(T[] value)
			{
				this.observer.OnNext(value);
			}

			// Token: 0x0600110C RID: 4364 RVA: 0x0004E874 File Offset: 0x0004CC74
			public override void OnError(Exception error)
			{
				try
				{
					this.observer.OnError(error);
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x0600110D RID: 4365 RVA: 0x0004E8AC File Offset: 0x0004CCAC
			public override void OnCompleted()
			{
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x04000A0A RID: 2570
			private readonly IObservable<T>[] sources;

			// Token: 0x04000A0B RID: 2571
			private readonly object gate = new object();

			// Token: 0x04000A0C RID: 2572
			private int completedCount;

			// Token: 0x04000A0D RID: 2573
			private int length;

			// Token: 0x04000A0E RID: 2574
			private T[] values;

			// Token: 0x02000338 RID: 824
			private class WhenAllCollectionObserver : IObserver<T>
			{
				// Token: 0x0600110E RID: 4366 RVA: 0x0004E8E4 File Offset: 0x0004CCE4
				public WhenAllCollectionObserver(WhenAllObservable<T>.WhenAll parent, int index)
				{
					this.parent = parent;
					this.index = index;
				}

				// Token: 0x0600110F RID: 4367 RVA: 0x0004E8FC File Offset: 0x0004CCFC
				public void OnNext(T value)
				{
					object gate = this.parent.gate;
					lock (gate)
					{
						if (!this.isCompleted)
						{
							this.parent.values[this.index] = value;
						}
					}
				}

				// Token: 0x06001110 RID: 4368 RVA: 0x0004E95C File Offset: 0x0004CD5C
				public void OnError(Exception error)
				{
					object gate = this.parent.gate;
					lock (gate)
					{
						if (!this.isCompleted)
						{
							this.parent.OnError(error);
						}
					}
				}

				// Token: 0x06001111 RID: 4369 RVA: 0x0004E9B0 File Offset: 0x0004CDB0
				public void OnCompleted()
				{
					object gate = this.parent.gate;
					lock (gate)
					{
						if (!this.isCompleted)
						{
							this.isCompleted = true;
							this.parent.completedCount++;
							if (this.parent.completedCount == this.parent.length)
							{
								this.parent.OnNext(this.parent.values);
								this.parent.OnCompleted();
							}
						}
					}
				}

				// Token: 0x04000A0F RID: 2575
				private readonly WhenAllObservable<T>.WhenAll parent;

				// Token: 0x04000A10 RID: 2576
				private readonly int index;

				// Token: 0x04000A11 RID: 2577
				private bool isCompleted;
			}
		}

		// Token: 0x02000339 RID: 825
		private class WhenAll_ : OperatorObserverBase<T[], T[]>
		{
			// Token: 0x06001112 RID: 4370 RVA: 0x0004EA4C File Offset: 0x0004CE4C
			public WhenAll_(IList<IObservable<T>> sources, IObserver<T[]> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.sources = sources;
			}

			// Token: 0x06001113 RID: 4371 RVA: 0x0004EA68 File Offset: 0x0004CE68
			public IDisposable Run()
			{
				this.length = this.sources.Count;
				if (this.length == 0)
				{
					this.OnNext(new T[0]);
					try
					{
						this.observer.OnCompleted();
					}
					finally
					{
						base.Dispose();
					}
					return Disposable.Empty;
				}
				this.completedCount = 0;
				this.values = new T[this.length];
				IDisposable[] array = new IDisposable[this.length];
				for (int i = 0; i < this.length; i++)
				{
					IObservable<T> observable = this.sources[i];
					WhenAllObservable<T>.WhenAll_.WhenAllCollectionObserver observer = new WhenAllObservable<T>.WhenAll_.WhenAllCollectionObserver(this, i);
					array[i] = observable.Subscribe(observer);
				}
				return StableCompositeDisposable.CreateUnsafe(array);
			}

			// Token: 0x06001114 RID: 4372 RVA: 0x0004EB2C File Offset: 0x0004CF2C
			public override void OnNext(T[] value)
			{
				this.observer.OnNext(value);
			}

			// Token: 0x06001115 RID: 4373 RVA: 0x0004EB3C File Offset: 0x0004CF3C
			public override void OnError(Exception error)
			{
				try
				{
					this.observer.OnError(error);
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x06001116 RID: 4374 RVA: 0x0004EB74 File Offset: 0x0004CF74
			public override void OnCompleted()
			{
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x04000A12 RID: 2578
			private readonly IList<IObservable<T>> sources;

			// Token: 0x04000A13 RID: 2579
			private readonly object gate = new object();

			// Token: 0x04000A14 RID: 2580
			private int completedCount;

			// Token: 0x04000A15 RID: 2581
			private int length;

			// Token: 0x04000A16 RID: 2582
			private T[] values;

			// Token: 0x0200033A RID: 826
			private class WhenAllCollectionObserver : IObserver<T>
			{
				// Token: 0x06001117 RID: 4375 RVA: 0x0004EBAC File Offset: 0x0004CFAC
				public WhenAllCollectionObserver(WhenAllObservable<T>.WhenAll_ parent, int index)
				{
					this.parent = parent;
					this.index = index;
				}

				// Token: 0x06001118 RID: 4376 RVA: 0x0004EBC4 File Offset: 0x0004CFC4
				public void OnNext(T value)
				{
					object gate = this.parent.gate;
					lock (gate)
					{
						if (!this.isCompleted)
						{
							this.parent.values[this.index] = value;
						}
					}
				}

				// Token: 0x06001119 RID: 4377 RVA: 0x0004EC24 File Offset: 0x0004D024
				public void OnError(Exception error)
				{
					object gate = this.parent.gate;
					lock (gate)
					{
						if (!this.isCompleted)
						{
							this.parent.OnError(error);
						}
					}
				}

				// Token: 0x0600111A RID: 4378 RVA: 0x0004EC78 File Offset: 0x0004D078
				public void OnCompleted()
				{
					object gate = this.parent.gate;
					lock (gate)
					{
						if (!this.isCompleted)
						{
							this.isCompleted = true;
							this.parent.completedCount++;
							if (this.parent.completedCount == this.parent.length)
							{
								this.parent.OnNext(this.parent.values);
								this.parent.OnCompleted();
							}
						}
					}
				}

				// Token: 0x04000A17 RID: 2583
				private readonly WhenAllObservable<T>.WhenAll_ parent;

				// Token: 0x04000A18 RID: 2584
				private readonly int index;

				// Token: 0x04000A19 RID: 2585
				private bool isCompleted;
			}
		}
	}
}
